define(function (require, exports, module) {
    var angular = require("angular");
    var ui = require('ui');
    var util = require('util');
    require("angular-ui");
    require("service/ng_service/environment");
    var api_prefix = G_CONFIG["api_prefix"] + "/qmyx/para-setting/"; //api前缀

    angular.module('PublicChannelIdentity', ['EnvironmentService'])
        .controller('ChannelIdentityController', ['$rootScope', '$scope', '$http', '$location', '$routeParams', 'environment',
            function ($rootScope, $scope, $http, $location, $routeParams, environment) {
                $scope.tabs = [
                    {
                        value: "partnerCompany",
                        name: '合作伙伴所属公司'
                    },
                    {
                        value: "agentCompany",
                        name: '代理公司所属公司'
                    },
                    {
                        value: "agencyCompany",
                        name: '中介公司所属公司'
                    }
                ];

                function getTabValue(tabValue) {
                    var tab;
                    for (var i = 0; tab = $scope.tabs[i]; i++) {
                        if (tab.value === tabValue) {
                            return tabValue;
                        }
                    }
                    return $scope.tabSelectedValue || $scope.tabs[0].value;
                }

                function getSelectedTab() {
                    var tabSelectedValue = $scope.tabSelectedValue;
                    var tabs = $scope.tabs;
                    var tab;
                    for (var i = 0; tab = tabs[i]; i++) {
                        if (tab.value === tabSelectedValue) {
                            return tab;
                        }
                    }
                    return null;
                }

                $scope.tabSelectedValue = getTabValue($routeParams.tab) || $scope.tabs[0].value;

                $scope.setTabSelected = function (tab) {
                    $scope.tabSelectedValue = tab.value;
                    $scope.tabList = $scope.data[tab.value];
                };

                $scope.setEnable = function (item, type) {
                    type = type ? type : getSelectedTab().value;
                    postData(
                        'enable-cim-data',
                        {
                            id: item.myParamValueId,
                            type: type
                        },
                        '设置成功',
                        true,
                        function () {
                            item.Disabled == '0';
                        }
                    )
                };

                $scope.setDefault = function (item, type) {
                    type = type ? type : getSelectedTab().value;
                    postData(
                        'set-cim-default',
                        {
                            id: item.myParamValueId,
                            type: type
                        },
                        '设置成功',
                        true,
                        function () {
                            item.IsDefault = '1';
                        });
                };

                $scope.edit = function (item) {
                    postData(
                        'edit-cim-data',
                        {
                            id: item.myParamValueId,
                            text: item.Text,
                            value: item.Value,
                            order: item.Order,
                            desc: item.Description
                        },
                        '新增成功',
                        true);
                };

                $scope.add = function (item, type, dialogModel, dialogScope) {
                    type = type ? type : getSelectedTab().value;
                    postData(
                        'add-cim-data',
                        {
                            id: item.myParamValueId,
                            text: item.Text,
                            value: item.Value,
                            order: item.Order,
                            desc: item.Description,
                            type: type
                        },
                        '新增成功',
                        true,
                        function (res) {
                            if (res.retCode == 0) {
                                dialogModel.close();
                            }
                        }
                    );

                    return false;
                };

                $scope.setDisable = function (item) {
                    util.confirm('确定禁用吗？', {
                        ok: function () {
                            postData('disable-cim-data', {
                                id: item.myParamValueId
                            }, '设置成功', true);
                        }
                    })
                };

                $scope.delete = function (item) {
                    util.confirm('确定删除吗？', {
                        ok: function () {
                            postData('del-cim-data', {
                                id: item.myParamValueId
                            }, '删除成功', true);
                        }
                    })
                };

                $scope.moveDown = function (item, type) {
                    var typeCache = type;
                    type = type ? type : getSelectedTab().value;
                    if (typeCache) {
                        var list = $scope.list;
                    } else {
                        var list = $scope.tabList;
                    }
                    var curPos = util.indexOf(item, list);
                    var curOrder = item.Order;
                    var NextOrder = list[curPos + 1].Order;
                    if (curOrder == NextOrder) {
                        if (curPos < list.length - 1) {
                            list[curPos] = list[curPos + 1];
                            list[curPos + 1] = item;
                            ui.showSucTip('下移成功');
                        }
                    } else {
                        postData(
                            'down-move',
                            {
                                id: item.myParamValueId,
                                type: type,
                                proId: ''
                            },
                            '下移成功',
                            false, function (res) {
                                if (typeof res.data === 'undefined') {
                                    getData();
                                }
                            });
                    }
                };

                $scope.moveUp = function (item, type) {
                    var typeCache = type;
                    type = type ? type : getSelectedTab().value;
                    if (typeCache) {
                        var list = $scope.list;
                    } else {
                        var list = $scope.tabList;
                    }
                    var curPos = util.indexOf(item, list);
                    var curOrder = item.Order;
                    var preOrder = list[curPos - 1].Order;
                    if (curOrder == preOrder) {
                        if (curPos > 0) {
                            list[curPos] = list[curPos - 1];
                            list[curPos - 1] = item;
                            ui.showSucTip('上移成功');
                        }

                    } else {
                        postData(
                            'up-move',
                            {
                                id: item.myParamValueId,
                                type: type,
                                proId: ''
                            },
                            '上移成功',
                            false,
                            function (res) {
                                if (typeof res.data === 'undefined') {
                                    getData();
                                }
                            });
                    }
                };

                $scope.reset = function (type) {
                    type = type ? type : getSelectedTab().value;
                    util.confirm('确定还原备选项吗？', {
                        ok: function () {
                            postData('reduction-option', {
                                type: type
                            }, '还原备选项成功', true);
                        }
                    })
                };

                /**
                 * @param {string} urlPath
                 * @param {Object} data
                 * @param {string} sucTip
                 * @param {boolean=} updateData
                 * @param {function=} sucCallback
                 */
                function postData(urlPath, data, sucTip, updateData, sucCallback) {
                    $http
                        .post(api_prefix + urlPath, data)
                        .success(function (res) {
                            var retCode = res.retCode;
                            if (retCode == 0) {
                                if (updateData) {
                                    getData();
                                }
                                sucCallback && sucCallback(res);

                                ui.showSucTip(sucTip);
                            } else {
                                ui.showErrTip(res.errMsg);
                            }
                        })
                }

                var isDev = false;

                $scope.showEditPop = function (data, dialog, dialogScope) {
                    dialogScope.isDev = isDev;
                };

                $scope.loading = false;
                /**
                 * 获取数据
                 */
                var getData = function () {
                    $scope.loading = true;
                    environment()
                        .then(function (res) {
                            isDev = res == 'dev';
                        })
                        .finally(function () {
                            $http
                                .get(api_prefix + "get-channel-identity-manage")
                                .then(function (res) {
                                    var retCode = res.data.retCode;
                                    if (retCode == 0) {
                                        var data = res.data.data;
                                        $scope.data = data;
                                        var list = data.brokerIdentity, tabList = data[getSelectedTab().value];
                                        for (var i = 0, item; item = list[i]; i++) {
                                            item.Order = parseInt(item.Order);
                                        }
                                        for (var i = 0, item; item = tabList[i]; i++) {
                                            item.Order = parseInt(item.Order);
                                        }
                                        list.sort(function (a, b) {
                                            return a.Order - b.Order
                                        });

                                        tabList.sort(function (a, b) {
                                            return a.Order - b.Order
                                        });
                                        $scope.list = list;
                                        $scope.tabList = tabList;
                                    } else {
                                        ui.showErrTip(res.data.errMsg);
                                        return false;
                                    }
                                })
                                .finally(function () {
                                    $scope.loading = false;
                                })
                        })
                };

                getData();
            }
        ])
});